home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / SpriteEngine / SE Programmed Sprite / SpriteHandlers.p < prev    next >
Encoding:
Text File  |  1995-04-04  |  3.1 KB  |  132 lines  |  [TEXT/MWPS]

  1. unit SpriteHandlers;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  8.         OSUtils, ToolUtils, OSEvents, 
  9. {$ENDC}
  10.         QDOffScreen, SpriteStructure, SpriteTools;
  11.  
  12.     procedure MoveSprite (theSprite: SpritePtr);
  13.     procedure HitSprite (theSprite: SpritePtr; anotherSprite: SpritePtr);
  14.     procedure InitSprites;
  15.  
  16. implementation
  17.  
  18. { Custom handlers - application dependent ***}
  19. {}
  20. {Edit this as necessary. It should always include the following three routines:}
  21. {}
  22. {MoveSprite: move the sprite}
  23. {}
  24. {HitSprite: handle collisions between two sprites}
  25. {}
  26. {InitSprites: Load all faces and create initial sprites }
  27.  
  28.  
  29. type
  30. ProgramPart = Record 
  31.     howLong:Integer;
  32.     dh:Integer;
  33.     dv:Integer;
  34. End;
  35.  
  36. var
  37. program1: array[0..6] of ProgramPart;
  38.  
  39.  
  40. procedure InitProgram1;
  41. begin
  42.     program1[0].howLong := 60;
  43.     program1[0].dh := 0;
  44.     program1[0].dv := 1;
  45.  
  46.     program1[1].howLong := 50;
  47.     program1[1].dh := 1;
  48.     program1[1].dv := 0;
  49.  
  50.     program1[2].howLong := 30;
  51.     program1[2].dh := 0;
  52.     program1[2].dv := -1;
  53.  
  54.     program1[3].howLong := 20;
  55.     program1[3].dh := -1;
  56.     program1[3].dv := 0;
  57.  
  58.     program1[4].howLong := 30;
  59.     program1[4].dh := 0;
  60.     program1[4].dv := -1;
  61.  
  62.     program1[5].howLong := 15;
  63.     program1[5].dh := -2;
  64.     program1[5].dv := 0;
  65.  
  66.     program1[6].howLong := -1;
  67.     program1[6].dh := -1;
  68.     program1[6].dv := -1;
  69. end; {InitProgram1}
  70.  
  71. var firstFace, secondFace, thirdFace:GrafPtr;
  72.  
  73.  
  74. Procedure MoveSprite(theSprite:SpritePtr);
  75.  
  76.      begin 
  77. theSprite^.position.h := theSprite^.position.h + program1[theSprite^.currentInstruction].dh;
  78.     theSprite^.position.v := theSprite^.position.v + program1[theSprite^.currentInstruction].dv;
  79.     theSprite^.timeOnCurrent := theSprite^.timeOnCurrent +1;;
  80.     If  (theSprite^.timeOnCurrent >= program1[theSprite^.currentInstruction].howLong) Then
  81.     
  82.          begin 
  83. theSprite^.timeOnCurrent := 0;
  84.         theSprite^.currentInstruction := theSprite^.currentInstruction + 1;
  85.         If  program1[theSprite^.currentInstruction].howLong < 0 Then
  86.             theSprite^.currentInstruction := 0;
  87.      End;
  88.     if KeepOnScreen(theSprite) then;
  89.  End; (*MoveSprite*)
  90.  
  91. Procedure HitSprite(theSprite:SpritePtr; anotherSprite:SpritePtr);
  92.  Begin  End; (*HitSprite*)
  93.  
  94.  
  95. Procedure InitSprites;
  96.  
  97.     var theSprite:SpritePtr;
  98.  
  99. (*Load all pictures*)
  100.      begin 
  101.     firstFace := LoadFaceFromCicn(128);            (*cicn resource #128.*)
  102.     secondFace := LoadFaceFromCicn(129);            (*cicn resource #129.*)
  103.     thirdFace := LoadFaceFromCicn(130);            (*cicn resource #130.*)
  104.  
  105. (*Create sprites*)
  106.     theSprite := NewSprite;
  107.     theSprite^.face := firstFace;
  108.     SetPt(theSprite^.position, 100, 100);
  109.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  110.     theSprite^.timeOnCurrent := 0;
  111.     theSprite^.currentInstruction := 0;
  112.  
  113.     theSprite := NewSprite;
  114.     theSprite^.face := secondFace;
  115.     SetPt(theSprite^.position, 50, 50);
  116.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  117.     theSprite^.timeOnCurrent := 0;
  118.     theSprite^.currentInstruction := 0;
  119.  
  120.     theSprite := NewSprite;
  121.     theSprite^.face := thirdFace;
  122.     SetPt(theSprite^.position, 150, 150);
  123.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  124.     theSprite^.timeOnCurrent := 0;
  125.     theSprite^.currentInstruction := 0;
  126.     
  127.     InitProgram1;
  128.  End; (*InitSprites*)
  129.  
  130.  end.
  131.  
  132.